﻿using DirectSell.Model.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Direct.IDAL.Message;


namespace DirectSell.DAL.User.Message
{
    /// <summary>
    /// 用户留言数据访问层
    /// </summary>
    public class MessageRepository:AbsRepository,IMessageRepository
    {
        #region 写入一条留言

        /// <summary>
        /// 写入一条留言
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool InsertMessage(MessageReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO Messages ");
            sb.Append(" ([SendCode] ");
            sb.Append(" ,[CreateDate] ");
            sb.Append(" ,[Title] ");
            sb.Append(" ,[MContent] ");
            sb.Append(" ,[SendName] ");
            sb.Append(" ,[SendPhone] ");
            sb.Append(" ,[ReciveCode] ");
            sb.Append("   ,[State]) ");
            sb.Append(" VALUES ");
            sb.AppendFormat(" ('{0}' ", req.SendCode);
            sb.AppendFormat(" ,'{0}' ", req.CreateDate);
            sb.AppendFormat(" ,'{0}' ", req.Title);
            sb.AppendFormat(" ,'{0}' ", req.MContent);
            sb.AppendFormat(" ,'{0}' ", req.SendName);
            sb.AppendFormat(" ,'{0}' ", req.SendPhone);
            sb.AppendFormat(" ,'{0}' ", req.ReciveCode);
            sb.AppendFormat(" ,'{0}') ", req.State);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 查询发送给我的留言
        /// <summary>
        /// 查询发送给我的留言
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<IList<Messages>> MyMessage(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" SELECT * FROM Messages WHERE ReciveCode='{0}' ORDER BY State ASC ", usercode);
            var result = await base.QueryAsync<Messages>(sb.ToString());
            return result as IList<Messages>;
        }
        #endregion

        #region 查询我发出的留言
        /// <summary>
        /// 查询我发出的留言
        /// </summary>
        /// <param name="usercode"></param>
        public async Task<IList<Messages>> AlreadyMessage(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" SELECT * FROM Messages WHERE SendCode='{0}' ORDER BY State ASC ", usercode);
            var result =  await base.QueryAsync<Messages>(sb.ToString());
            return result as IList<Messages>;
        }
        #endregion

        #region 删除一条留言
        /// <summary>
        /// 删除一条留言
        /// </summary>
        /// <returns></returns>
        public bool DeleteMessage(int id)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" DELETE FROM Messages WHERE Id = {0} ", id);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 查一条留言
        /// <summary>
        /// 查一条留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Messages> QueryMsg(int id)
        {
            string str = string.Format(" SELECT * FROM Messages WHERE Id ={0} ", id);
            var info = await base.QueryAsync<Messages>(str);
            return info.FirstOrDefault();
        }
        #endregion

        #region 标为已读
        /// <summary>
        /// 标为已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool AlRead(int id)
        {
            string str = string.Format(" UPDATE Messages SET State=1 WHERE Id ={0} AND State=0 ", id);
            return base.Execute(str);
        }
        #endregion

        #region 查询是否有未读留言
        /// <summary>
        /// 查询是否有未读留言
        /// </summary>
        /// <returns></returns>
        public async Task<bool> QueryExist()
        {
            string str = "SELECT COUNT(*) FROM Messages WHERE State=0 AND ReciveCode='admin'";
            var result = await base.QueryFirstTAsync<int>(str);
            return result > 0;
        }
        #endregion
    }
}
